import 'dart:io';
import 'package:dio/dio.dart';

class PgyerService {
  static var dio = Dio();
  static var API_Key = '78c83aa2cc2846845ff30092040a3b3a';
  static var User_Key = '53501a1f3f484c10398677d1b30150e1';
  static var App_Key = Platform.isAndroid ? '0a4be551aeea0212a2d4aed11759f43f' : '';
  static var baseServer = 'https://www.pgyer.com';

  static Future<Response<dynamic>> post(String path, Map<String, dynamic>? body, Map<String, dynamic>? headers) async {
    var data = getData();
    if(body != null) {
      data.addAll(body);
    }
    return await dio.post(
      baseServer + path,
      data: data,
      options: Options(
        contentType: Headers.formUrlEncodedContentType,
        headers: headers
      )
    );
  }


  static Future<Response<dynamic>> put(String path, Map<String, dynamic>? body, Map<String, dynamic>? headers) async {
    var data = getData();
    if(body != null) {
      data.addAll(body);
    }
    return await dio.put(
      baseServer + path,
      data: data,
      options: Options(
        contentType: Headers.formUrlEncodedContentType,
        headers: headers
      )
    );
  }

  static Future<Response<dynamic>> get(String path, Map<String, dynamic>? queryParameters, Map<String, dynamic>? headers) async {
    var data = getData();
    if(queryParameters != null) {
      data.addAll(queryParameters);
    }
    return await dio.get(
      baseServer + path,
      queryParameters: data,
      options: Options(
        contentType: Headers.formUrlEncodedContentType,
        headers: headers
      )
    );
  }

  static Future<Response<dynamic>> delete(String path, Map<String, dynamic>? queryParameters, Map<String, dynamic>? headers) async {
    var data = getData();
    if(queryParameters != null) {
      data.addAll(queryParameters);
    }
    return await dio.delete(
      baseServer + path,
      queryParameters: data,
      options: Options(
        contentType: Headers.formUrlEncodedContentType,
        headers: headers
      )
    );
  }

  static Map<String, dynamic> getData() {
    return {
      "_api_key": API_Key,
      "appKey": App_Key
    };
  }
}
